package org.eclipse.soc.ide4edu.assignment.wizards;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AssignmentDownloadWizardPage2 extends WizardPage {

	Composite composite;
	Button thruBrowser;
	Button thruEclipse;
	Text usernameField;
	Text passwordField;

	public AssignmentDownloadWizardPage2(String pageName) {
		super(pageName);
		setTitle(pageName);
	}

	protected void checkNull(ModifyEvent e) {

		if (e.widget instanceof Text) {
			Text temp = (Text) e.widget;
			String choice = null;

			if (e.widget == usernameField)
				choice = "Username";

			else if (e.widget == passwordField)
				choice = "Password";

			if (temp.getText().length() == 0)
				updateStatus(choice + " is Empty");
			else
				updateStatus(null);
		}
	}

	public void createControl(Composite parent) {

		composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		Group loginGroup = new Group(composite, SWT.NONE);
		loginGroup.setText("Authentication Info");
		layout = new GridLayout();
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		loginGroup.setLayout(layout);
		loginGroup.setLayoutData(gd);

		thruBrowser = new Button(loginGroup, SWT.RADIO);
		thruBrowser.setText("Authenticate through Web Interface (Secure)");
		thruBrowser.setSelection(true);
		thruBrowser.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleChoice(e);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		thruBrowser.setLayoutData(gd);

		thruEclipse = new Button(loginGroup, SWT.RADIO);
		thruEclipse.setText("Authenticate through Eclipse (Plain Text)");
		thruEclipse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleChoice(e);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		thruEclipse.setLayoutData(gd);

		createLoginInfo(loginGroup);
		disableLoginInfo();
		composite.layout();
		setControl(composite);
	}

	private void createLoginInfo(Group loginGroup) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		loginGroup.setLayout(layout);

		GridData gd = new GridData();
		gd.widthHint = 250;
		new Label(loginGroup, SWT.NONE).setText("Username");
		usernameField = new Text(loginGroup, SWT.SINGLE | SWT.BORDER);
		usernameField.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				checkNull(e);
			}

		});
		usernameField.setLayoutData(gd);

		new Label(loginGroup, SWT.NONE).setText("Password");
		passwordField = new Text(loginGroup, SWT.SINGLE | SWT.BORDER);
		passwordField.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				checkNull(e);
			}

		});
		passwordField.setLayoutData(gd);

		disableLoginInfo();
	}

	private void disableLoginInfo() {

		usernameField.setEnabled(false);
		passwordField.setEnabled(false);

		composite.layout();

	}

	private void enableLoginInfo() {
		usernameField.setEnabled(true);
		passwordField.setEnabled(true);

		composite.layout();
	}

	public String getPassword() {
		return passwordField.getText();
	}

	public String getUserChoice() {
		if (thruBrowser.getSelection() == true) {
			return "browser";
		} else
			return "eclipse";
	}

	public String getUsername() {
		return usernameField.getText();
	}

	protected void handleChoice(SelectionEvent e) {
		if (e.widget == thruBrowser) {
			disableLoginInfo();

		}
		if (e.widget == thruEclipse) {
			enableLoginInfo();

		}

	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

}
